package ext.tianma.part.forms;


import java.io.File;
import java.rmi.RemoteException;
import java.util.Hashtable;
import org.apache.commons.lang.StringUtils;
import ext.com.iba.IBAUtil;
import ext.generic.erp.attributes.EnumerationUtil;
import ext.tianma.bom.util.ClassificationUtil;
import ext.tianma.bom.util.LWCUtil;
import ext.tianma.bom.util.StringUtil;
import ext.tianma.part.PartDescriptionUtil;
import ext.tianma.util.IbaAttributeUtil;
import ext.tianma.util.PropertiesUtil;
import wt.part.WTPart;
import wt.util.WTException;

/**
 * 生成并设置物料描述
 * 
 * @author laichao_sun
 *
 */
public class DescriptionUntil {
	public static String Path = "codebase" + File.separator + "ext" + File.separator + "tianma"
			+ File.separator + "part" + File.separator + "config"+File.separator+"Description.properties";

	public static void setPartDescription(WTPart part) throws WTException {
		try {
		if (part == null) {
			return;
		}
		String classification2 = ClassificationUtil.getClassification(part);
		if (StringUtil.isNullOrEmpty(classification2)) {
			return;
		}
		if(!classification2.contains("MDM")) {
			return;
		}
		if (TMTypeUtil.isFinishedGood(part)) {
			String classification = ClassificationUtil.getClassification(part);
			if (StringUtil.isNullOrEmpty(classification)) {
				return;
			}
			// 成品
			String name = ClassificationUtil.getClassificationNode(part).getName();
			setDescription(part, name);
		} else if (TMTypeUtil.isSemiFinishedGood(part)) {
			// 半成品
			String SemiFinishedGoodType = IbaAttributeUtil.getAttributeValue(part, "SemiFinishedGoodType");
			setDescription(part, SemiFinishedGoodType);
		} else if (TMTypeUtil.isRawMaterial(part)) {
			// 原材料
			// 中英文名称
			String partCNAndENName = IbaAttributeUtil.getAttributeValue(part, "partCNAndENName");
			if (StringUtil.isNullOrEmpty(partCNAndENName)) {
				// 如果为上片
				if (TMTypeUtil.isTopPolarizer(part)) {
					partCNAndENName = "Polarizer -- 偏光片TOP";
					// 如果为下片
				} else if (TMTypeUtil.isBotPolarizer(part)) {
					partCNAndENName = "Polarizer -- 偏光片BOTTOM";
				} else if (TMTypeUtil.isPhantomPolarizer(part)) {
					partCNAndENName = "Dummy Polarizer -- 偏光片虚拟组件";
				} else if (TMTypeUtil.isPolarizer(part)) {
					partCNAndENName = "Polarizer -- 偏光片";
				}
			}
			setDescription(part, partCNAndENName);
		} else if (TMTypeUtil.isTwoLevelMaterial(part)) {
			// 二級物料
		}
		}catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static void setDescription(WTPart part, String classNodeName) throws WTException, RemoteException {
		PropertiesUtil ut = new PropertiesUtil(Path);
		String ibaKey = ut.getValueByKey(classNodeName);
		String[] keys = "".equals(ibaKey) || ibaKey == null ? null : ibaKey.split(";");// 2、why？
		if (!(keys == null)) {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < keys.length; i++) {
				String key = keys[i];
				if(StringUtil.isNullOrEmpty(key)) {
					continue;
				}
				String string;
				if ("name".equals(key)) {
					string = part.getName();
				} else {
					//string = (String) IBAUtil.getIBAValue(part, cell);
					string = LWCUtil.getValue(part, key)+"";
				}
				System.out.println("SLC ==物料描述功能开发字段值==> "+i+" | "+string);
				if(StringUtil.isNullOrEmpty(string)) {
					continue;
				}
				if("NA".equals(string)) {
					continue;
				}
				if (i == 0) {
					sb.append(string);
				} else {

					if ("SizeHNew".equals(key) || "SizeWideNew".equals(key)
							|| "SizeThicknessNew".equals(key) || "SizeWNew".equals(key)
							|| "SizeTNew".equals(key) || "GlassSizeWNew1".equals(key)
							|| "GlassSizeTNew".equals(key)) {
						sb.append("×");
						if ("SizeHNew".equals(key) || "SizeLNew".equals(key)
								|| "SizeWNew".equals(key) || "SizeTNew".equals(key)
								|| "diameter".equals(key) || "GlassSizeTNew".equals(key)
								|| "GlassSizeWNew1".equals(key) || "GlassSizeL1".equals(key)
								|| "SizeLengthNew".equals(key) || "SizeThicknessNew".equals(key)
								|| "SizeWideNew".equals(key)) {
							sb.append(string);
							sb.append("mm");
						} else if ("WeightNew1".equals(key)) {
							sb.append(string);
							sb.append("g");
						} else if ("DiameterSizeNew".equals(key)) {
							sb.append(string);
							sb.append("μm");
						} else if ("ResValueK".equals(key)) {
							sb.append(string);
							sb.append("KΩ");
						} else if ("Integration".equals(key) || "IntegrationNew".equals(key)
								|| "LayoutNumNew".equals(key)) {
							sb.append(string);
							sb.append("PANEL");
						} else {
							sb.append(string);
						}
					} else {
						if ("beforeCfTp".equals(key) || "afterCfTp".equals(key)) {
							sb.append("+");
						} else {
							sb.append("-");
						}
						if ("SizeHNew".equals(key) || "SizeLNew".equals(key)
								|| "SizeWNew".equals(key) || "SizeTNew".equals(key)
								|| "diameter".equals(key) || "GlassSizeTNew".equals(key)
								|| "GlassSizeWNew1".equals(key) || "GlassSizeL1".equals(key)
								|| "SizeLengthNew".equals(key) || "SizeThicknessNew".equals(key)
								|| "SizeWideNew".equals(key)) {
							sb.append(string);
							sb.append("mm");
						} else if ("WeightNew1".equals(key)) {
							sb.append(string);
							sb.append("g");
						} else if ("DiameterSizeNew".equals(key)) {
							sb.append(string);
							sb.append("μm");
						} else if ("ResValueK".equals(key)) {
							sb.append(string);
							sb.append("KΩ");
						} else if ("Integration".equals(key) || "IntegrationNew".equals(key)
								|| "LayoutNumNew".equals(key)) {
							sb.append(string);
							sb.append("PANEL");
						} else {
							sb.append(string);
						}
					}
				
				}
			}
			if (TMTypeUtil.isRawMaterial(part)) {
				if (PartDescriptionUtil.checkEffectiveSite(part)) {
					String environCategory = getEnvironCategory(part);
					sb.append(environCategory);
				}
				String departments = (String) IBAUtil.getIBAValue(part, "departments");
				if (!StringUtil.isNullOrEmpty(departments)) {
					sb.append("-");
					sb.append(departments);
				}
			}
			if (!StringUtil.isNullOrEmpty(sb.toString())) {
				Hashtable<String, String> ibamap = new Hashtable<String, String>();
				ibamap = addToHashtable(ibamap, "partDescription", sb.toString());
				IBAUtil.setSoftTypeIBAValues(part, ibamap);
			}
		}
	}

	private static String getEnvironCategory(WTPart part) throws WTException, RemoteException {
		// 环保类别
		String ibaValue = (String) IBAUtil.getIBAValue(part, "partEnvProtectionCategory");
		String environCategory = "";
		if (ibaValue != null && !ibaValue.isEmpty()) {
			String environValue = EnumerationUtil.getEnumerationDisplayValue("环保类别", ibaValue);
			if (StringUtils.isNotBlank(environValue)) {
				environCategory = "(" + environValue + ")";
			}
		}
		return environCategory;
	}

	/**
	 * 实现Hashtable添加数据时不会出现空指针异常
	 * 
	 * @param hashtable
	 * @param key
	 * @param value
	 * @return
	 */
	private static Hashtable<String, String> addToHashtable(Hashtable<String, String> hashtable, String key,
			String value) {
		if (key == null && hashtable != null) {
			return hashtable;
		}

		if (value == null && hashtable != null) {
			return hashtable;
		}

		if (key == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (value == null && hashtable == null) {
			return new Hashtable<String, String>();
		}

		if (hashtable == null) {
			hashtable = new Hashtable<String, String>();
		}
		hashtable.put(key, value);

		return hashtable;
	}
}
